package com.shiguiwu.springboot3.service;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONUtil;
import com.shiguiwu.springboot3.entity.User;
import com.shiguiwu.springboot3.mapper.UserMapper;
import com.shiguiwu.springboot3.starter.service.MyService;
//import io.seata.spring.boot.autoconfigure.properties.config.ConfigNacosProperties;
//import io.seata.spring.boot.autoconfigure.properties.registry.RegistryNacosProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: user服务
 * @author: stone
 * @date: Created by 2023/7/2 20:49
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springboot3.service
 */
@Service
@Slf4j
@RefreshScope
public class UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MyService myService;

    //@Autowired(required = false)
    //private ConfigNacosProperties configNacosProperties;
    //
    //@Autowired(required = false)
    //private RegistryNacosProperties registryNacosProperties;

    @Value("${service.vgroupMapping.my_test_tx_group:}")
    private String defaultString;

    @Value("${my.aaa:}")
    private String aaa;

    @Transactional
    public Integer test() {
        //log.info("配置的属性信息 ===> {}, ===> {},registryNacosProperties {}", JSONUtil.toJsonStr(myService), defaultString,registryNacosProperties);
        log.error("配置的list信息 ===> {}", JSONUtil.toJsonStr(myService.getMyProperties().getList()));
        log.error("配置的a信息 ===> {}", aaa);

        ex();
        userMapper.saveA(new ArrayList<>(),1);
        return userMapper.queryTest();
    }

    @Transactional(rollbackFor = Exception.class)
    public Integer testBatch() {

        List<User> users = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            User user = new User();
            user.setAge(1);
            user.setName("aa");


            users.add(user);
        }
        userMapper.batchInsert(users);
        return userMapper.queryTest();
    }

    public void ex() {
        Example example = new Example(User.class);

        example.createCriteria()
                .andEqualTo("name", "shiguiwu");
        example.orderBy("age");

        List<User> users = userMapper.selectByExample(example);

        User a = new User();
        a.setAge(RandomUtil.randomInt(1,100));
        a.setName(RandomUtil.randomInt(1,100) +"xx");

        a.setRead(RandomUtil.randomInt(1,10));
        userMapper.insertSelective(a);



        Example example1 = new Example(User.class);

        example1.createCriteria()
                .andEqualTo("read", 1);
        example1.orderBy("age");

        List<User> users1 = userMapper.selectByExample(example1);
        //
        userMapper.selectByCC();

        userMapper.selectByBB();
    }
}
